package cn.chenfei.jfx.login;

import cn.chenfei.jfx.App;
import cn.chenfei.jfx.common.JFUtils;
import javafx.animation.FadeTransition;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.stage.Popup;
import javafx.util.Duration;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

/**
 * @author ChenFei
 * @date 2022/4/13
 */
public class Login_5 extends StackPane {

    private VBox container = new VBox();

    {
        this.setPadding(new Insets(10, 10, 10, 10));
        this.setBackground(null);
        this.getChildren().add(container);
        this.setAlignment(container, Pos.CENTER);
        container.getStyleClass().add("container");
        this.getStylesheets().add(App.class.getResource("css/Login_5.css").toExternalForm());
        //拖动
        JFUtils.dragCurrentWindow(container);
    }

    //顶部位置
    private HBox topBox = new HBox();
    //最小化，关闭窗口按钮
    private Button hideBut = new Button();
    private Button closeBut = new Button();

    {
        this.getChildren().addAll(topBox);
        this.setAlignment(topBox, Pos.TOP_RIGHT);
        topBox.setMaxWidth(0);
        topBox.setMaxHeight(0);
        topBox.getStyleClass().add("top-box");
        topBox.getChildren().addAll(hideBut, closeBut);
        hideBut.setGraphic(new FontIcon(AntDesignIconsOutlined.MINUS));
        closeBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CLOSE));
        hideBut.getStyleClass().add("hide-but");
        closeBut.getStyleClass().add("close-but");
        hideBut.setOnMouseClicked(event -> JFUtils.setIconified(event.getSource(),true));
        closeBut.setOnMouseClicked(event -> JFUtils.closeCurrentWindow(event.getSource()));
    }


    private Pane logo = new Pane();
    private Label titleLabel = new Label("xxx平台");

    {
        container.getChildren().addAll(logo, titleLabel);
        logo.getStyleClass().add("logo");
        titleLabel.getStyleClass().add("title-label");

    }

    /**
     * 输入框布局-----------------------------------------------------------
     */
    private AnchorPane userApBox = new AnchorPane();
    private HBox userBox = new HBox();
    private Label userLabel = new Label();
    private TextField userField = new TextField();
    private Label userTitle = new Label("账号");
    //
    private AnchorPane pswApBox = new AnchorPane();
    private HBox pswBox = new HBox();
    private Label pswLabel = new Label();
    private PasswordField pswField = new PasswordField();
    private Label pswTitle = new Label("密码");
    //登录
    private VBox loginBox = new VBox();
    private Button loginBut = new Button("登录");
    //服务条款
    private HBox fwBox = new HBox();
    private Label fwLabel = new Label("登录即表示同意此");
    private Hyperlink fwLink = new Hyperlink("服务条款");

    {
        container.getChildren().addAll(userApBox, pswApBox, loginBox, fwBox);
        userApBox.getChildren().addAll(userBox, userTitle);
        pswApBox.getChildren().addAll(pswBox, pswTitle);
        userApBox.setLeftAnchor(userTitle, 40.0);
        userApBox.setTopAnchor(userTitle, -10.0);
        pswApBox.setLeftAnchor(pswTitle, 40.0);
        pswApBox.setTopAnchor(pswTitle, -10.0);
        pswTitle.setVisible(false);
        userTitle.setVisible(false);
        userApBox.getStyleClass().add("user-ap-box");
        pswApBox.getStyleClass().add("psw-ap-box");
        userTitle.getStyleClass().add("user-title");
        pswTitle.getStyleClass().add("psw-title");
        //
        userBox.getChildren().addAll(userLabel, userField);
        userBox.setHgrow(userField, Priority.ALWAYS);
        userBox.getStyleClass().add("user-box");
        userLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.USER));
        userLabel.getStyleClass().add("user-label");
        userField.setPromptText("账号");
        userField.getStyleClass().add("user-field");
        //
        pswBox.getChildren().addAll(pswLabel, pswField);
        userBox.setHgrow(pswField, Priority.ALWAYS);
        pswBox.getStyleClass().add("psw-box");
        pswLabel.setGraphic(new FontIcon(AntDesignIconsOutlined.LOCK));
        pswLabel.getStyleClass().add("psw-label");
        pswField.getStyleClass().add("psw-field");
        pswField.setPromptText("密码");
        //
        loginBox.getChildren().add(loginBut);
        loginBox.getStyleClass().add("login-box");
        loginBut.getStyleClass().add("login-but");
        //服务条款
        fwBox.getChildren().addAll(fwLabel, fwLink);
        fwBox.getStyleClass().add("fw-box");
        fwLabel.getStyleClass().add("fw-label");
        fwLink.getStyleClass().add("fw-link");

    }

    //其他登录方式按钮
    private HBox thirdBox = new HBox();
    private Button ddBut = new Button();
    private Button wxBut = new Button();
    private Button zfbBut = new Button();

    {
        container.getChildren().addAll(thirdBox);
        thirdBox.getChildren().addAll(ddBut, wxBut, zfbBut);
        thirdBox.setMaxWidth(0);
        thirdBox.getStyleClass().add("third-box");
        ddBut.getStyleClass().add("dd-but");
        wxBut.getStyleClass().add("wx-but");
        zfbBut.getStyleClass().add("zfb-but");
        //提示语
        Tooltip tooltip = new Tooltip("钉钉账号登录");
        tooltip.setShowDelay(Duration.millis(50));
        ddBut.setTooltip(tooltip);
    }

    //底部内容
    private HBox bottomBox = new HBox();
    private Label forget = new Label("忘记密码");
    private Label email = new Label("邮箱登录");
    private Label register = new Label("快速注册");

    {
        this.getChildren().add(bottomBox);
        this.setAlignment(bottomBox, Pos.BOTTOM_CENTER);
        bottomBox.getChildren().addAll(forget, email, register);
        bottomBox.setMaxHeight(0);
        bottomBox.getStyleClass().add("bottom-box");
        forget.getStyleClass().add("forget");
        email.getStyleClass().add("email");
        register.getStyleClass().add("register");
    }

    public Login_5() {
        //输入框选中事件绑定
        userField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "green-color;" : "green-thin-color;";
            userBox.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.setStyle(String.format("-fx-border-color:%s", color));
            userLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
            userTitle.setVisible(t1);
        });
        pswField.focusedProperty().addListener((observableValue, aBoolean, t1) -> {
            String color = t1 ? "green-color;" : "green-thin-color;";
            pswBox.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.setStyle(String.format("-fx-border-color:%s", color));
            pswLabel.getGraphic().setStyle(String.format("-fx-icon-color:%s", color));
            pswTitle.setVisible(t1);
        });
        //服务条款
        fwLink.setOnMouseClicked(event -> {
            //可以在此打开一个窗口显示服务条款，展示给用户看
        });
        //登录点击事件
        loginBut.setOnMouseClicked(event -> {
            event.consume();
            if (Cursor.WAIT.equals(loginBut.getCursor())) {
                return;
            }
            String username = userField.getText();
            String password = pswField.getText();
            System.out.println(String.format("username: %s ; password: %s ;", username, password));
            if (username.trim().equals("")) {
                messageShow("账号不能为空");
                return;
            }
            if (password.trim().equals("")) {
                messageShow("密码不能为空");
                return;
            }
            loginBut.setCursor(Cursor.WAIT);
        });

    }

    /**
     * 弹出消息提示框
     *
     * @param mess
     */
    private void messageShow(String mess) {
        //提示框
        Popup messagePopup = new Popup();
        Label message = new Label(mess);
        //提示框样式
        messagePopup.getContent().add(message);
        message.setPrefHeight(100);
        message.setPrefWidth(300);
        message.setAlignment(Pos.CENTER);
        message.setStyle("-fx-background-color:rgb(255,255,255);" +
                "-fx-text-fill:red;" +
                "-fx-background-radius: 4px;" +
                "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.3), 20.0, 0, 0, 0);");
        message.setFont(Font.font(18));
        //显示提示框
        messagePopup.show(App.getStage(), 10, 10);
        //动画
        FadeTransition scale = new FadeTransition(Duration.seconds(1), message);
        scale.setFromValue(1);
        scale.setToValue(0);
        scale.setOnFinished(actionEvent -> messagePopup.hide());
        new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Platform.runLater(() -> scale.play());
        }).start();
    }

}
